﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Common;
using MarginTrading.Services;

namespace MarginTrading.Forms
{
    public partial class QuickAddClients : Form
    {
        int iCurrentRow = -1;
        public QuickAddClients()
        {
            InitializeComponent();
        }

        private void QuickAddClients_Load(object sender, EventArgs e)
        {
            DataTable table = new DataTable();
            table.Columns.Add();
            table.Columns[0].ColumnName = "Client ID";
            dataGridView.DataSource = table;
            dataGridView.ReadOnly = false;
            dataGridView.Width = 150;
            dataGridView.Columns[0].Width = 148;
            dataGridView.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            if (validateInputs() == false)
            {
                return;
            }

            bool bError = false;
            for (int iI = 0; iI < dataGridView.Rows.Count; iI++)
            {
                if (dataGridView.Rows[iI].Cells[0].Value == null)
                {
                    continue;
                }
                Client client = new Client();
                client.Code = dataGridView.Rows[iI].Cells[0].Value.ToString();
                client.Agent = textBoxAgent.Text;
                if (textBoxMarginLimit.Text.Length != 0)
                {
                    client.MarginLimit = double.Parse(textBoxMarginLimit.Text);
                }
                client.RegDate = dateTimePicker1.Value;

                try
                {
                    ClientService.Add(client);
                }
                catch (System.Exception ex)
                {
                    bError = true;
                }
            }

            if (bError == true)
            {
                MessageBox.Show("Operation completed with errors", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            MessageBox.Show("Operation completed successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            
            Close();
        }

        private void dataGridView_SelectionChanged(object sender, EventArgs e)
        {
            //if (iCurrentRow != -1)
            //{
            //    MessageBox.Show(dataGridView.Rows[iCurrentRow].Cells[0].Value.ToString());
            //}
            //iCurrentRow = dataGridView.CurrentRow.Index;
            //MessageBox.Show("current row " + iCurrentRow.ToString());
        }

        private bool validateInputs()
        {
            return true;
        }
    }
}
